<?php

namespace AuctionCore\Common;

use AuctionCore\System\AuctionException;

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

/**
 * 发送手机短信功能类
 * @author       爱民
 */
class MobMsg {

    private $apikey = "39d0d4be2ea6df430503158d61488bae";
    private $api_url = "http://yunpian.com/v1/sms/tpl_send.json";
    private $types = array(
        '1' => array('tpl_id' => '1124141', 'var_list' => array('username' => '', 'password' => '')) //通知车主登陆用户中心查看拍卖结果
        , '2' => array('tpl_id' => '1124143', 'var_list' => array('address' => '', 'emp' => '', 'mob' => '', 'date' => '')) //通知车主到店交车验车
        , '3' => array('tpl_id' => '1124155', 'var_list' => array('per' => '')) //提醒车主查收首款
        , '4' => array('tpl_id' => '1124159', 'var_list' => array('per' => '')) //提醒车主查收尾款
        , '5' => array('tpl_id' => '1124163', 'var_list' => array()) //通知车主领取过户手续
        , '6' => array('tpl_id' => '1124165', 'var_list' => array('order_no' => '')) //通知车商拍卖成功
        , '7' => array('tpl_id' => '1124167', 'var_list' => array('order_no' => '', 'datetime' => '')) //通知车商及时到店验车付款
        , '8' => array('tpl_id' => '1124169', 'var_list' => array('order_no' => '', 'price' => '')) //通知车商由于违约已扣除违约金
        , '9' => array('tpl_id' => '1124171', 'var_list' => array('username' => '', 'password' => '')) //通知车商由于违约已扣除违约金
        , '10' => array('tpl_id' => '1124175', 'var_list' => array('add_price' => '', 'remain' => '')) //车商充值保证金后成功提醒
        , '11' => array('tpl_id' => '1178591', 'var_list' => array('scene_name' => '', 'carlist' => '')) //车商群发短信通知
        , '12' => array('tpl_id' => '1281455', 'var_list' => array('code' => '',)) // 手机验证码
        , '13' => array('tpl_id' => '1281447', 'var_list' => array('code' => '')) //注册验证码
        , '14' => array('tpl_id' => '1281451', 'var_list' => array('code' => '')) //找回密码验证码
        , '15' => array('tpl_id' => '1281455', 'var_list' => array('code' => '')) //车来车往验证码
        , '16' => array('tpl_id' => '1330857', 'var_list' => array('mob' => '', 'pwd' => '')) //审核成功创建用户发送给用户账户密码
        , '17' => array('tpl_id' => '1330859', 'var_list' => array()) //审核成功发送给用户提醒信息
        , '18' => array('tpl_id' => '1350031', 'var_list' => array('realname' => '', 'username' => '', 'password' => '')) //4s账号添加通知
        , '19' => array('tpl_id' => '1350033', 'var_list' => array('realname' => '', 'username' => '', 'password' => '')) //集团账号添加通知
    );

    /**
     * 调用短信网关，发送短信
     * 记入发送日志
     * 成功返回：code值，否则false
     * @param    String $mobile    目标手机号
     * @param    array  [type:'短信类型',var_list[模板变量列表]]
     * @return   true/false;
     */
    public function send($mobile, $data) {
        if (!$mobile) {
            AuctionException::make_throw('800025');
        }

        $apikey = $this->apikey;
        $tpl_id = $this->types[$data['type']]['tpl_id'];
        if (!$tpl_id || !is_numeric($tpl_id)) {
            AuctionException::make_throw('800023');
        }
        $var_list = $data['var_list'];
        $diff_key = array_diff_key($this->types[$data['type']]['var_list'], $var_list);
        if (count($diff_key) > 0) {
            AuctionException::make_throw('800024');
        }
        $var_list = array_intersect_key($var_list, $this->types[$data['type']]['var_list']);
        $var_list_tpl = array();
        foreach ($var_list as $key => $value) {
            $var_list_tpl['#' . $key . '#'] = $value;
        }
        $var_list_tpl = http_build_query($var_list_tpl);
        $tpl_value = $var_list_tpl;
        //$tpl_value = '#text#='.$text;
        //$tpl_id = 1;
        //$tpl_value = '#code#=xxxsssx&#company#=云片网';

        $url = $this->api_url;
        $mobile = urlencode("$mobile");
        $encoded_tpl_value = urlencode($tpl_value);  //tpl_value需整体转义        
        $post_string = "apikey=$apikey&tpl_id=$tpl_id&tpl_value=$encoded_tpl_value&mobile=$mobile";
        $re = NetCom::sock_post($url, $post_string);
        $re = json_decode($re);
        
        $sms_log['phone'] = $mobile;
        $sms_log['sms_type'] = $data['type'];
        $sms_log['sms_val_list'] = json_encode($var_list);
        if (intval($re->code) > 0) {
            $sms_log['is_success'] = 2;
            $this->add_sms_log($sms_log);
            \Think\Log::record(date('Y-m-d H:i:s') . '发送短信失败！,mobile:' . $mobile . ",tpl_id:" . $tpl_value . "\n");
            return false;
        } else {
            $this->add_sms_log($sms_log);
            return true;
        }
    }

    /**
     * 短信发送日志
     */
    public function add_sms_log($data) {
        return M('sms_log')->add($data);
    }

    //发送验证码
    public function send_code($mobile, $code) {
        $apikey = $this->apikey;
        $tpl_id = 1;
        $tpl_value = '#company#=爱搜二手车&#code#=' . $code;

        $url = $this->api_url;
        $mobile = urlencode("$mobile");
        $encoded_tpl_value = urlencode($tpl_value);  //tpl_value需整体转义        
        $post_string = "apikey=$apikey&tpl_id=$tpl_id&tpl_value=$encoded_tpl_value&mobile=$mobile";

        $re = NetCom::sock_post($url, $post_string);
        $re = json_decode($re);
        if ($re && intval($re->code) == 0) {
            return true;
        } else {
            if ($re->code == 2) {
                AuctionException::make_throw('800022');
            } else {
                AuctionException::make_throw('800021');
            }

            \Think\Log::record(date('Y-m-d H:i:s') . '发送短信失败！,mobile:' . $mobile . ",tpl_id:" . $tpl_value . "\n");

            return false;
        }
    }

}

?>